﻿using System;
using System.Web;
using System.Web.SessionState;

namespace Marbles.Core.Web.Syndication
{
    public class FeedHandler : IHttpHandler, IRequiresSessionState
    {

        public bool IsReusable
        {
            get { return false; }
        }

        public virtual void FeedRequest(object Sender, FeedHandlerEventArgs e)
        {
            if (e.Request.Params["type"] != null)
            {
                switch (e.Request.Params["type"].ToLower())
                {
                    case "rss": e.Feed = new RSS.RSSFeed(); break;
                    case "atom": e.Feed = new Atom.AtomFeed(); break;
                }
            }
            else
            {
                e.Feed = new RSS.RSSFeed();
            }

            e.Feed.Title = "This feed is intended as a Placeholder and is not for Public Consumption.";
        }

        public void ProcessRequest(HttpContext context)
        {
            FeedHandlerEventArgs e = new FeedHandlerEventArgs(context);
            FeedRequest(this, e);

            if ((e.Cancel) || (e.Feed == null)) { throw new HttpException(404, "Feed Not Found."); }

            FeedWriter.Write(context.Response, e.Feed);
        }

    }
}
